﻿using UnityEngine;
using System.Collections;
using com.lib.events;
namespace com.lib.net
{
    public class HttpNetWork : MonoBehaviour
    {
        public static HttpNetWork Instance = null;

        /// <summary>
        /// obj
        /// </summary>
        private string URL = string.Empty;

        private NetWorks.NetCall call;

        void Awake()
        {
            Instance = this;
            DontDestroyOnLoad(this.gameObject);
        }


        public void getHttpData(string url, NetWorks.NetCall call)
        {

            StartCoroutine(getHttpDatas(url, call));

        }


        IEnumerator getHttpDatas(string url, NetWorks.NetCall call)
        {
            WWW www = new WWW(url);
            yield return www;
            if(www.error == null)
            {
                call(www.bytes);
            }
            else
            {
                call(null);
            }
        }


        public void getHttpTexture(string url, NetWorks.NetCall call)
        {

            StartCoroutine(getHttpTextures(url, call));
        }

        IEnumerator getHttpTextures(string url, NetWorks.NetCall call)
        {
            WWW www = new WWW(url);
            yield return www;
            if (www.error == null)
            {
                call(www.texture);
            }
            else
            {
                call(null);
            }

        }
        

        /// <summary>
        /// 发送
        /// </summary>
        public void Send(object data)
        {
            if (NetData.HttpInit == false)
            {
                Logger.LogError("Please Call NetWorks.Init At NetWorks Line 28");
                return;
            }
            StartCoroutine(SendData(data));
        }

        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        IEnumerator SendData(object data)
        {
            WWW www = new WWW(URL + data);
            yield return www;
            if (www.error == null)
            {

                byte[] array = www.bytes;
                CallBack(true, NetWorks.Instance.ResDecode(array));
            }
            else
            {
                Logger.LogError("Data At Server:" + URL + "  Is ERROR Data=" + data + "  At Line 50");
                CallBack(false, null);
            }

        }


        /// <summary>
        /// 初始化连接地址,初始化注册接口
        /// </summary>
        /// <param name="url"></param>
        public void Init(string url, NetWorks.NetCall tempCall)
        {
            URL = url;
            call = tempCall;
            NetData.HttpInit = true;
        }

        /// <summary>
        /// 回调事件
        /// </summary>
        private void CallBack(bool tr, object o)
        {
            if (tr == false)
            {
                NetCallData nc = new NetCallData();
                nc.cmd = -1;
                call(nc);
                return;
            }
            try
            {
                var data = ToolsLib.getJsonList(o.ToString());
                foreach (var mdata in data)
                {
                    NetCallData nc = new NetCallData();
                    nc.cmd = mdata.Get("C").getInt();
                    nc.state = mdata.Get("S").getInt();
                    nc.value = mdata;
                    call(nc);
                }
            }
            catch (System.Exception e)
            {
                Logger.LogError("Msg Exend Fail On HttpNetWork AT Line 93 Please Check Data =>" + o.ToString() + "   Error:" + e.Message);
                NetCallData nc = new NetCallData();
                nc.cmd = -1;
                call(nc);
            }
        }

    }
}